import java.util.LinkedList;
import java.util.Queue;

/**
 * 面试题09相关题目：用两个队列实现栈
 */
public class Offer_09_II {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(2);
        System.out.println(myStack.top());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
        System.out.println(myStack.pop());
        System.out.println(myStack.empty());
    }
}

class MyStack1 {
    // queueA 用于存储栈内的元素，queueB 作为入栈操作的辅助队列
    Queue<Integer> queueA, queueB;

    /** Initialize your data structure here. */
    public MyStack1() {
        queueA = new LinkedList<>();
        queueB = new LinkedList<>();
    }

    /** Push element x onto stack. */
    public void push(int x) {
        // 首先将元素入队到 queueB
        queueB.offer(x);
        // 然后将 queueA 的全部元素依次出队并入队到 queueB
        while (!queueA.isEmpty()) {
            queueB.offer(queueA.poll());
        }
        // 再将 queueA 和 queueB 互换
        Queue<Integer> temp = queueA;
        queueA = queueB;
        queueB = temp;
    }

    /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        return queueA.poll();
    }

    /** Get the top element. */
    public int top() {
        return queueA.peek();
    }

    /** Returns whether the stack is empty. */
    public boolean empty() {
        return queueA.isEmpty();
    }
}

/**
 * Your MyStack object will be instantiated and called as such: MyStack obj =
 * new MyStack(); obj.push(x); int param_2 = obj.pop(); int param_3 = obj.top();
 * boolean param_4 = obj.empty();
 */